<template>
  <div class="positRel">
    <div class="course-content">
      <Card>
        <div class="choose">
          <div class="course-center-top mb_20">
            <div class="top-text mr_20">
              <Icon type="md-appstore" size="16" :color="activeColor" />
              <span class="activeStyle head-btn">全部课程</span>
            </div>
            <div class="top-text mr_20">
              <Icon type="ios-bookmark-outline" size="16" />
              <span class="head-btn" @click="goMyCource('mycourse')">我的课程</span>
            </div>
            <!-- 类型选择区域开始 -->
            <div class="top-text">
              <span>课程名称/教师搜索：</span>
              <Input
                v-model="params.expertName"
                icon="ios-search-outline"
                clearable
                placeholder="请输入..."
                :style="inputStyle"
                @on-click="getConditionQuery"
                @on-search="getConditionQuery"
                @on-clear="getConditionQuery"
                search
              />
            </div>
          </div>
          <div class="select-section mb_20">
            <span class="select-title">课程类型：</span>
            <Tag
              v-for="(tagItem, index) in courseTypeList"
              :key="index"
              size="large"
              color="error"
              class="mr_20"
              :name="tagItem.name"
              checkable
              :checked="tagItem.checked"
              @on-change="checkedHandle(index, 'courseTypeList')"
              style="cursor: pointer"
              >{{ tagItem.name }}</Tag
            >
          </div>
          <div class="select-section mb_20">
            <span class="select-title">人&emsp;&emsp;群：</span>
            <Tag
              v-for="(tagItem, index) in courseTypeList2"
              :key="index"
              :class="tagItem.checked ? 'ivu-tag-checked' : ''"
              size="large"
              color="error"
              class="mr_20"
              :name="tagItem.name"
              checkable
              :checked="tagItem.checked"
              @on-change="checkedHandle(index, 'courseTypeList2')"
              style="cursor: pointer"
              >{{ tagItem.name }}</Tag
            >
          </div>

          <!-- 排序开始 -->
          <div class="select-section2 mb_20">
            <div
              class="tabs-btn"
              :class="isList == 1 ? 'activeStyle' : ''"
              @click="handleShow(1)"
            >
              <Icon type="ios-apps-outline" size="20" />
              橱窗
            </div>
            <div
              class="tabs-btn"
              :class="isList == 2 ? 'activeStyle' : ''"
              @click="handleShow(2)"
            >
              <Icon type="ios-list" size="22" />
              列表
            </div>
            <div
              class="mr_20"
              @click="setSort('')"
              style="display: flex; align-items: center"
            >
              <span :style="params.sortField == '' ? 'color:#F4333C' : 'color:#63676E'"
                >综合排序</span
              >
              <img
                :src="
                  params.sortField == ''
                    ? require('@/assets/base/sorted.png')
                    : require('@/assets/base/sort.png')
                "
                style="
                  width: calc(7 / 1920 * 100vw);
                  height: calc(11 / 1920 * 100vw);
                  margin-left: calc(6 / 1920 * 100vw);
                "
              />
              <!-- 上下箭头组件 -->
            </div>
            <div
              class="mr_20"
              @click="setSort('grade')"
              style="display: flex; align-items: center"
            >
              <span
                :style="params.sortField == 'grade' ? 'color:#F4333C' : 'color:#63676E'"
                >好评</span
              >
              <img
                :src="
                  params.sortField == 'grade'
                    ? require('@/assets/base/sorted.png')
                    : require('@/assets/base/sort.png')
                "
                style="
                  width: calc(7 / 1920 * 100vw);
                  height: calc(11 / 1920 * 100vw);
                  margin-left: calc(6 / 1920 * 100vw);
                "
              />
            </div>
            <div
              class="mr_20"
              @click="setSort('course_watch_count')"
              style="display: flex; align-items: center"
            >
              <span
                :style="
                  params.sortField == 'course_watch_count'
                    ? 'color:#F4333C'
                    : 'color:#63676E'
                "
                >热度</span
              >
              <img
                :src="
                  params.sortField == 'course_watch_count'
                    ? require('@/assets/base/sorted.png')
                    : require('@/assets/base/sort.png')
                "
                style="
                  width: calc(7 / 1920 * 100vw);
                  height: calc(11 / 1920 * 100vw);
                  margin-left: calc(6 / 1920 * 100vw);
                "
              />
            </div>
            <div
              class="mr_20"
              @click="setSort('learn_num')"
              style="display: flex; align-items: center"
            >
              <span
                :style="
                  params.sortField == 'learn_num' ? 'color:#F4333C' : 'color:#63676E'
                "
                >销量</span
              >
              <img
                :src="
                  params.sortField == 'learn_num'
                    ? require('@/assets/base/sorted.png')
                    : require('@/assets/base/sort.png')
                "
                style="
                  width: calc(7 / 1920 * 100vw);
                  height: calc(11 / 1920 * 100vw);
                  margin-left: calc(6 / 1920 * 100vw);
                "
              />
            </div>
          </div>
        </div>

        <!-- 排序结束 -->

        <!-- 橱窗 -->
        <ul class="mycourse-list" v-show="isList == 1">
          <li
            class="mycourse-item"
            v-for="item in courseList"
            :key="item.id"
            @click="handleTo(item.isCharge, item.payStatus, item.id, item.seriesName)"
          >
            <p v-if="item.pageTop == '1'" class="topping">置顶</p>
            <div
              class="mycourse-img"
              :style="`background-image:url(${item.coverPath})`"
            ></div>
            <div class="mycourse-info">
              <p class="text">{{ item.seriesName }}</p>
            </div>
            <div class="teacher-box">
              <div class="teacher-face">
                <img :src="item.expertFace || 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'" />
                <span>{{ item.expertName }} </span>
              </div>
              <div class="price">
                <span class="btn free" v-if="item.isCharge === '0'">免费</span>
                <span v-if="item.isCharge === '1'">
                  <span class="btn haveBought" v-if="item.payStatus == 'PAID'"
                    >已购买</span
                  >
                  <span class="price" v-if="item.payStatus === 'UNPAID'"
                    ><span>￥</span>{{ item.price ? item.price.toFixed(2) : "0.0" }}</span
                  ></span
                >
              </div>
            </div>
          </li>
        </ul>
        <!-- 列表 -->

        <ul class="list-box" v-show="isList == 2">
          <li
            class="list-item"
            v-for="item in courseList"
            :key="item.title"
            @click="handleTo(item.isCharge, item.payStatus, item.id, item.seriesName)"
          >
            <div class="list-head">
              <img :src="item.coverPath" />
              <p v-if="item.pageTop == '1'" class="topping">置顶</p>
            </div>
            <div class="list-text">
              <p class="name">{{ item.seriesName }}</p>

              <div class="teacher-box">
                <div class="teacher-face">
                  <img :src="item.expertFace" />
                  <span>{{ item.expertName }} </span>
                </div>
              </div>
              <p class="look">{{ item.courseWatchCount || 0 }}人看过</p>
            </div>
            <div class="list-other">
              <span class="btn free" v-if="item.isCharge === '0'">免费</span>
              <span v-if="item.isCharge == '1'">
                <span class="btn haveBought" v-if="item.payStatus == 'PAID'">已购买</span>
                <span class="price" v-if="item.payStatus === 'UNPAID'"
                  ><span>￥</span>{{ item.price ? item.price.toFixed(2) : "0.0" }}</span
                >
              </span>
            </div>
          </li>
        </ul>

        <empty v-if="courseList.length === 0" />
        <div class="page">
          <Page
            :total="total"
            :page-size="params.rows"
            size="small"
            show-sizer
            show-total
            show-elevator
            @on-change="currentChange"
            @on-page-size-change="pageSizeChange"
          />
        </div>
      </Card>
    </div>
    <Footer :top="20"></Footer>
  </div>
</template>

<script>
import NavArrow from "@/components/course/nav-arrow-component.vue";
import Footer from "@/components/footer/my_footer.vue";
import { getCourseList, getCourseType, getConditionQuery } from "../../api/otwoo.js";
export default {
  components: {
    NavArrow,
    Footer,
  },
  data() {
    return {
      isList: 1,
      size: [15, 30, 45, 60], //page分页
      activeColor: "#f4333c",
      inputStyle: "width: 250px;height: 32px;border-radius: 5px;",
      tagColor: "#fff",
      cardWidth: "",
      listWidth: "",
      num: "1",
      type: "付款",
      courseTypeList: [],
      courseTypeList2: [],
      courseList: [], //课程列表
      //查询参数
      params: {
        categoryStr: "",
        courseCategory: "",
        expertName: "",
        keyword: "",
        page: 1,
        rows: 10,
        sortField: "", //排序
        sortType: "desc",
        userId: "",
      },
      //数据总条数
      total: 0,
    };
  },
  mounted() {
    //获取课程列表
    this.getConditionQuery();
    //获取类型
    getCourseType().then((res) => {
      this.courseTypeList = res.result["课程类型"].map((item) => {
        item.checked = false;
        return item;
      });
      this.courseTypeList2 = res.result["人群"].map((item) => {
        item.checked = false;
        return item;
      });
    });
  },
  computed: {
    getWidth() {
      this.listWidth = (this.cardWidth - 40) / 3;
    },
  },
  methods: {
    handleShow(type) {
      this.isList = type;
    },
    //跳转详情
    handleTo(isCharge, payStatus, id, name) {
      this.$router.push({
        name: "course_detail",
        query: { isCharge: isCharge, payStatus: payStatus, id: id, name },
      });
    },
    //标签选中事件
    checkedHandle(index, name) {
      this[name][index].checked = !this[name][index].checked;
      this.getConditionQuery();
    },

    //根据不同删选获取不同的课程列表
    getConditionQuery() {
      let typeId = [];
      this.courseTypeList.forEach((item) => {
        if (item.checked) typeId.push(item.id);
      });
      this.courseTypeList2.forEach((item) => {
        if (item.checked) typeId.push(item.id);
      });
      this.params.courseCategory = typeId.join(",");
      //根据不同删选获取不同的课程列表
      getConditionQuery(this.params).then((res) => {
        this.courseList = res.result.rows;
        this.total = Number(res.result.total);
      });
    },
    // 点击排序
    setSort(type) {
      if (this.params.sortField == type) {
        this.params.sortType = this.params.sortType == "desc" ? "asc" : "desc";
      } else {
        this.params.sortField = type;
        this.params.sortType = 'desc';
      }
      this.params.page = 1;
      this.getConditionQuery();
    },
    //改变页码
    currentChange(current) {
      this.params.page = current;
      this.getConditionQuery();
    },
    //改变每页显示条数
    pageSizeChange(size) {
      this.params.rows = size;
      this.getConditionQuery();
    },
    //跳转我的课程
    goMyCource() {
      this.$router.push({
        path: "/myCourse",
      });
      this.$store.commit("SET_PERLEFTMENUSHOW", true);
      sessionStorage.setItem("name", "myCourse");
    },
  },

  beforeDistory() {
    window.removeEventListener("resize", function () {
      console.log("移除了");
    });
  },
};
</script>

<style lang="scss" scoped>
$titleColor: #52555a;
$baseColor: #e1e1e1;
$activeColor: #f4333c;
$baseFont: 14px;
.course-content {
  min-height: calc(100vh - (80 / 1920 * 100vw));
  padding: 0px calc(150 / 1920 * 100vw);
  margin-top: calc(54 / 1920 * 100vw);
  padding-bottom: 50px;
  .course-card-list {
    display: flex;
    flex-wrap: wrap;
    margin-top: calc(26 / 1920 * 100vw);
    .course-card {
      width: calc(296 / 1920 * 100vw);
      box-shadow: calc(4 / 1920 * 100vw) 0px calc(10 / 1920 * 100vw) #efeded;
      padding-bottom: calc(18 / 1920 * 100vw);
      border-radius: calc(5 / 1920 * 100vw);
      overflow: hidden;
      cursor: pointer;
      .shop-face,
      .course-face {
        width: calc(296 / 1920 * 100vw);
        height: calc(170 / 1920 * 100vw);
        background-size: cover;
      }
      .information {
        padding: 0px calc(10 / 1920 * 100vw);
        display: flex;
        flex-direction: column;
        .introduce {
          display: block;
          line-height: calc(22 / 1920 * 100vw);
          margin-top: calc(12 / 1920 * 100vw);
          font-size: calc(14 / 1920 * 100vw);
          overflow: hidden;
          display: -webkit-box;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 2;
          -ms-text-overflow: ellipsis;
          text-overflow: ellipsis;
        }
      }
    }
  }
  .choose {
    padding: 0px calc(30 / 1920 * 100vw);
  }

  .teacher-box {
    padding: 20px 10px;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .price {
      font-size: calc(12 / 1920 * 100vw);
      color: #f4333c !important;
    }
  }
  .page {
    width: 100%;
    text-align: right;
    padding: 0 20px 20px 0;
  }
}
.course-center-top {
  display: flex;
  align-items: center;
  font-size: $baseFont;
  .top-text {
    display: flex;
    align-items: center;
    font-family: "SourceHanSansCN";
    color: $titleColor;
    font-weight: 700;
    .head-btn {
      cursor: pointer;
    }
  }
}
.activeStyle {
  color: $activeColor;
}
.select-section,
.select-section2 {
  display: flex;
  align-items: center;
  font-size: $baseFont;
  .tabs-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    margin-right: 15px;
    img {
      width: 18px;
      height: 16px;
      margin-right: 5px;
    }
  }
  .select-title {
    font-family: "SourceHanSansCN";
    color: $titleColor;
    font-weight: 700;
  }
  // 属性样式
  .ivu-tag-error {
    width: 102px;
    height: 32px;
    line-height: 32px;
    text-align: center;
    border-radius: 5px;
    color: $baseColor;
    border: 1px solid #e8eaec !important;
  }
  .ivu-tag-checked {
    border: none;
  }
}
.select-section2 {
  > div {
    cursor: pointer;
  }
}
.price {
  margin-left: auto;
  display: block;
  font-size: calc(18 / 1920 * 100vw);
  color: #f4333c !important;
  margin-top: calc(14 / 1920 * 100vw);
  > span {
    font-size: calc(12 / 1920 * 100vw);
  }
}
// 橱窗样式
.mycourse-list {
  display: flex;
  flex-wrap: wrap;
  //   justify-content: space-between;
  padding: 0 20px;
  .mycourse-item {
    width: 19%;
    margin: 0 1% 20px 0;
    background: #ffffff;
    box-shadow: 0px 4px 10px 0px #efeded;
    border-radius: 5px;
    position: relative;
    overflow: hidden;
    .topping {
      position: absolute;
      left: calc(-25 / 1920 * 100vw);
      top: calc(15 / 1920 * 100vw);
      width: calc(110 / 1920 * 100vw);
      height: calc(25 / 1920 * 100vw);
      line-height: calc(25 / 1920 * 100vw);
      text-align: center;
      color: #fff;
      font-size: calc(10 / 1920 * 100vw);
      background-color: #fb6d74;
      transform: rotate(320deg);
      transform-origin: center;
    }
    .mycourse-img {
      height: 170px;
      overflow: hidden;
      border-radius: 5px 5px 0 0;
      background-size: cover;
      cursor: pointer;
    }
    .mycourse-info {
      padding: 14px 10px 0 10px;
      min-height: 50px;
      .text {
        margin-bottom: 14px 10px 0 14px;
        overflow: hidden;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 2;
        -ms-text-overflow: ellipsis;
        text-overflow: ellipsis;
        cursor: pointer;
      }
    }
    &:nth-child(5n) {
      margin-right: 0;
    }
  }
}
.teacher-face {
  display: flex;
  align-items: center;
  > img {
    width: calc(24 / 1920 * 100vw);
    height: calc(24 / 1920 * 100vw);
    border-radius: calc(12 / 1920 * 100vw);
  }
  > span {
    font-size: calc(12 / 1920 * 100vw);
    color: #63676e;
    margin-left: calc(6 / 1920 * 100vw);
  }
}
// 列表区
.list-box {
  padding: 0 20px;
  .list-item {
    position: relative;
    padding: 15px 20px;
    border-bottom: 1px solid #ddd;
    display: flex;
    .list-head {
      width: 20%;
      height: 120px;
      border-radius: 4px;
      overflow: hidden;
      position: relative;
      cursor: pointer;
      .topping {
        position: absolute;
        left: calc(-25 / 1920 * 100vw);
        top: calc(15 / 1920 * 100vw);
        width: calc(110 / 1920 * 100vw);
        height: calc(25 / 1920 * 100vw);
        line-height: calc(25 / 1920 * 100vw);
        text-align: center;
        color: #fff;
        font-size: calc(10 / 1920 * 100vw);
        background-color: #fb6d74;
        transform: rotate(320deg);
        transform-origin: center;
      }
      img {
        width: 100%;
        height: 100%;
        background-size: cover;
      }
    }
    .list-text {
      margin-left: 20px;
      flex: 1;
      max-width: 60%;
      position: relative;
      .name {
        font-size: 16px;
        font-weight: bold;
        color: #333333;
        margin-bottom: 10px;
        cursor: pointer;
      }
      .desc {
        color: #a1a1a1;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
      }
      .look {
        position: absolute;
        bottom: 0;
      }
    }
    .list-other {
      position: absolute;
      right: 10%;
      bottom: 15px;
    }
  }
}
.btn {
  display: inline-flex;
  justify-content: center;
  align-content: center;
  padding: 4px 8px;
  background: #ff900d;
  border-radius: 3px;
}
.haveBought {
  background: #ffeedb !important;
  color: #ff900d !important;
}
.free {
  background: #dbf9f9;
  color: #0bd7d7;
}
// 筛选区
.select-title {
  display: flex;
  align-items: center;
}
.btn-box {
  position: absolute;
  top: 10px;
  right: 0;
}

// 标签的样式
.ivu-tag-cyan,
.ivu-tag-orange {
  font-weight: 500;
  border-radius: 3px;
  border: none;
  line-height: 22px;
}
:deep(.ivu-card-body) {
  padding: calc(40 / 1920 * 100vw) 0 0;
}
</style>
